package com.example.project.manageController;

import com.example.project.dao.UserDAO;
import com.example.project.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("manage")
public class ManageViewUserController {
    private final UserDAO userDAO = new UserDAO();
    @RequestMapping("viewuser")
    public String viewuser(Model model) {

        List<User> userList = userDAO.findAll();
        model.addAttribute("userList", userList);
        return "manage/viewuser";
    }
    @RequestMapping("resetpassword")

    public String resetpassword(HttpServletRequest request, HttpServletResponse response)
    {
        String id=request.getParameter("x");
        String newpassword=request.getParameter("y");
        int index=Integer.parseInt(id);
        UserDAO userDAO=new UserDAO();

        boolean flag=userDAO.ResetPassword(newpassword,index);
        try {
            PrintWriter out=response.getWriter();
            out.print(String.valueOf(flag));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/manage/viewuser";

    }
    @RequestMapping("findUserByCondition")
    public String findUserByCondition(String select,String test,Model model)
    {

        List <User> userList =new ArrayList<>();
        switch (select){

            case "用户名称":
                 userList=userDAO.findUserByUserName(test);
                break;
            case "用户电话":
               User user= userDAO.findUserByUserPhone(test);
               userList.add(user);
                break;
        }
        model.addAttribute("userList", userList);
        return "manage/viewuser";

    }

}
